<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>管理登录/Management sign in</title>
    <link rel="stylesheet" href="/asset/bulma.min-82aac43507618108.css">
    <link rel="stylesheet" href="/asset/fontawesome.min-5e9e696c59c57e83.css">
    <link rel="stylesheet" href="/asset/solid.min-70c2e5caa950974d.css">
    <script type="text/javascript">
        function login() {
            const user = {
                password: document.getElementById('admin_password').value,
                captcha: document.getElementById('captcha').value,
            };
            const options = {
                method: 'POST',
                body: JSON.stringify(user),
                headers: {
                    'Content-Type': 'application/json'
                }
            };
            fetch('/management/login', options)
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                    if (data.status === 0 && data.data === true) {
                        document.cookie = 'X-SONGDAY-USER-AUTHED=true; SameSite=Lax; Path=/;';
                        let gotoUrl = '/management';
                        if (location.search) {
                            const s = '.redirect_url=';
                            let p = location.search.indexOf(s);
                            if (p > -1) {
                                gotoUrl = location.search.substring(p + s.length);
                                p = gotoUrl.indexOf('&');
                                if (p > -1)
                                    gotoUrl = gotoUrl.substring(0, p);
                                gotoUrl = decodeURIComponent(gotoUrl);
                            }
                        }
                        location.href = gotoUrl;
                    } else {
                        document.getElementById('errorMessage').value = data.error.detail;
                        document.getElementById('errorMessage').style.display = 'block';
                    }
                })
                .catch(err => {
                    console.log(err);
                });
        }
    </script>
</head>
<body>
<div class="container">
    <h1 class="title">
        管理登录/Management sign in
    </h1>
    <p>&nbsp;</p>
    <div class="field">
        <label class="label">密码/Password</label>
        <div class="control is-medium has-icons-left has-icons-right">
            <input class="input" type="password" placeholder="" id="admin_password" value=""/>
            <span class="icon is-small is-left">
              <i class="fas fa-lock"></i>
            </span>
        </div>
        <p class="help"><a href="/management/forgot-password">忘记密码/Forgot password</a></p>
    </div>
    <div class="field">
        <label class="label">验证码/Captcha</label>
        <div class="control is-medium has-icons-left has-icons-right">
            <input class="input" type="text" placeholder="" id="captcha" value=""/>
            <span class="icon is-small is-left">
              <i class="fas fa-lock"></i>
            </span>
        </div>
        <div><img src="/tool/verify-image"/></div>
    </div>
    <div class="field is-grouped">
        <div class="control">
            <button class="button is-link" onclick="login();">登录/Sign in</button>
        </div>
        <div class="control">
            <button class="button is-link is-light" onclick="location.href='/';">返回/Back</button>
        </div>
    </div>
</div>
</body>
</html>